const EventEmitter = require('events');
const eventEmitter = new EventEmitter();

//1.设置监听，本身不会运行，只有eventEmitter运行的时候，此方法才会运行。
eventEmitter.on('CustomContent',()=>{
    console.log('CustomContent event has occured');
});
eventEmitter.on('CustomContent',(num1,num2)=>{
    console.log(num1 + num2);
});

//2.使CustomContent运行，才会log出CustomContent event has occured
eventEmitter.emit('CustomContent');
eventEmitter.emit('CustomContent',1,2);//3

//3.监听类
class Person extends EventEmitter{
    constructor(name){
        super();
        this._name = name;
    }
    get name(){
        return this._name;
    }
}
let newPerson1 = new Person('Jack');
let newPerson2 = new Person('Mary');
newPerson1.on('name',()=>{
    console.log('my name is ' + newPerson1.name)
});
newPerson2.on('name',()=>{
    console.log('my name is ' + newPerson2.name)
});
newPerson1.emit('name');//my name is Jack
newPerson2.emit('name');//my name is Mary